function promiseAll(promiseArray) {
  return new Promise((resolve, reject) => {
    // console.log(typeof promiseArray); 返回的是Object类型
    if (!Array.isArray(promiseArray)) {
      return reject(new Error('传入的参数必须是数组'))
    }

    const res = []
    const promiseNums = promiseArray.length;
    let counter = 0

    for (let i = 0; i < promiseNums; i++) {
      const pro = promiseArray[i]
      // 即使数组元素不是promise对象 resolve会强转为promise对象
      Promise.resolve(pro).then(value => {
        counter++
        // 保证依然元素有序
        res[i] = value

        // 异步任务必须放在这里判断
        if (counter === promiseNums) {
          resolve(res)
        }
      }).catch(err => reject(err))
    }
  })

}
const p1 = new Promise((resolve, reject) => {
  setTimeout(() => resolve(1), 1000)
})

const p2 = new Promise((resolve, reject) => {
  setTimeout(() => resolve(2), 2000)
})

const p3 = new Promise((resolve, reject) => {
  setTimeout(() => resolve(3), 3000)
})

const proAll = promiseAll([p1, p2, p3])
  .then(res => console.log(res))
  .catch(err => console.log(err))